<?php

App::uses('AppController', 'Controller');

class ScrapsController extends AppController
{
    public $name = 'Scraps';
    public $uses = array('Scrap');

    function index()
    {
        $scraps = $this->Scrap->find('all', array('order'=>'created DESC'));
        $this->set('scraps',$scraps);
//        die(debug($scraps));
    }
    
    function last()
    {
        //die(debug($_POST));
        $this->layout = 'ajax';
        
        
        // Verifica se tem última ID
        if($_POST['id_last'] == 0)
        {
            $scrap = $this->Scrap->find('first', array('order'=>'created ASC'));
        }
        else
        {
            $scrap = $this->Scrap->find('first', array('order'=>'created ASC', 'conditions'=>array('Scrap.id >'=>$_POST['id_last'])));
        }
        
        $this->set('scrap',$scrap);
        
        
        // Se for a última foto, escreve: %nada%
        if(empty($scrap))
        {
            $last_scrap = 'ß'.$id.'ß^';
        }
        
        //se não, continua
        else
        {
            if(!empty($scrap['Image']['dir']))
            {
                 if($scrap['Scrap']['type']=='image')
                 {
                     $last_scrap = 'ß'.$scrap['Scrap']['id'].
                        'ß'.$scrap['User']['name'].
                        'ß'.$scrap['User']['id'].
                        'ßimage'.
                        'ß'.$scrap['Image']['description'].
                        'ßhttp://'.Configure::read('app_domain').'/phoreapp/'.$scrap['Image']['dir'].'/'.$scrap['Image']['filename'];
                 }
                 else if($scrap['Scrap']['type']=='video')
                 {
                     $last_scrap = 'ß'.$scrap['Scrap']['id'].
                        'ß'.$scrap['User']['name'].
                        'ß'.$scrap['User']['id'].
                        'ßvideo'.
                        'ß'.$scrap['Image']['description'].
                        'ßhttp://'.Configure::read('app_domain').'/phoreapp/'.$scrap['Image']['dir'].'/'.$scrap['Image']['filename'];
                 }
            }
           

            else
            {
                $last_scrap = 'ß'.$scrap['Scrap']['id'].
                        'ß'.$scrap['User']['name'].
                        'ß'.$scrap['User']['id'].
                        'ßmessage'.
                        'ß'.$scrap['Message']['description'].
                        'ß_';
            }
        }
        
        echo ($last_scrap);
    }
}
